package gameCard

import (
	"common"
	"framework/remote"
	"time"
)

type MessageReq struct {
	Type int         `json:"type"`
	Data MessageData `json:"data"`
}
type MessageData struct {
	Operate            OperateType `json:"operate"`
	Type               int         `json:"type"`               //-1=出售,1=安装怪兽卡,2吞噬,3=升级,4安装防御武器卡
	CardKey            int         `json:"cardKey"`            //卡牌下标
	CardID             uint        `json:"cardID"`             //卡牌id
	CardIDTypeID       int         `json:"cardIDTypeID"`       //卡牌分类id
	JustCardKey        int         `json:"justCardKey"`        //卡牌下标
	JustCardID         uint        `json:"justCardID"`         //卡牌id
	JustCardIDTypeID   int         `json:"justCardIDTypeID"`   //卡牌分类id
	Source             int         `json:"source"`             //来源 1=手牌,2=场地
	DeputyCardKey      int         `json:"deputyCardKey"`      //副卡牌下标
	DeputyCardID       uint        `json:"deputyCardID"`       //副卡牌id
	DeputyCardIDTypeID int         `json:"deputyCardIDTypeID"` //副卡牌分类id
	DeputySource       int         `json:"deputySource"`       //副来源 1=手牌,2=场地
}
type OperateType int

const (
	OperateTypeNone             OperateType = iota //无
	OperateTypeBuy                                 //购买卡牌
	OperateTypeRefresh                             //刷新
	OperateTypeUpgrade                             //升级
	OperateTypeSkill                               //技能
	OperateTypeSellCards                           //出售牌
	OperateTypeInstallCard                         //安装怪兽卡
	OperateTypeEvolutionCard                       //吞噬卡
	OperateTypeUpgradeCard                         //升级卡
	OperateTypeInstallEquipCard                    //安装装备卡
)

type Card struct {
	CardID              uint     `json:"cardID"`              //卡牌id
	CardIDTypeID        int      `json:"cardIDTypeID"`        //卡牌分类id
	CardName            string   `json:"cardName"`            //卡牌名称
	CardImg             string   `json:"cardImg"`             //卡牌图片
	CardDesc            string   `json:"cardDesc"`            //卡牌简介
	CardType            CardType `json:"cardType"`            //卡牌类型
	Attack              int64    `json:"attack"`              //攻击力
	AdditionalAttack    int64    `json:"additionalAttack"`    //附加攻击力
	TotalAttack         int64    `json:"totalAttackAttack"`   //总攻击力
	Blood               int64    `json:"blood"`               //血量
	AdditionalBlood     int64    `json:"additionalBlood"`     //附加血量
	TotalBlood          int64    `json:"totalBlood"`          //总血量
	SurplusBlood        int64    `json:"surplusBlood"`        //剩余血量
	Potential           int64    `json:"potential"`           //潜力
	AdditionalPotential int64    `json:"additionalPotential"` //附加潜力
	TotalPotential      int64    `json:"totalPotential"`      //总潜力
	Price               int64    `json:"price"`               //价格
	LinkType            CardType `json:"linkType"`            //关联的类型
	Consume             int64    `json:"consume"`             //消耗的卡牌数
	Level               int64    `json:"level"`               //等级
	Star                int64    `json:"star"`                //星级
}

type RefreshCard struct {
	Card  Card `json:"card"`
	IsBuy bool `json:"isBuy"` //是否购买
}

type CardType int //卡牌类型

const (
	CardTypeNone CardType = iota
	Ordinary              //怪兽卡 - 3
	Evolution             //吞噬卡 - 3
	Armour                //防具卡 - 3
	Weapon                //武器卡 - 3
	Upgrade               //升级卡 - 4
)

// MonstersCard 场地怪兽卡
type MonstersCard struct {
	Card              //怪兽卡
	TotalPrice int64  `json:"totalPrice"` //总价
	Arms       Card   `json:"arms"`       //武器牌
	Defense    Card   `json:"defense"`    //防御牌
	UpCard     []Card `json:"upCard"`     //升级卡
	Evolution  struct {
		Monsters Card   `json:"monsters"` //怪兽卡
		Upgrade  Card   `json:"upgrade"`  //吞噬卡
		UpCard   []Card `json:"upCard"`   //升级卡
	} `json:"evolution"` //吞噬卡
}

type SkillType int //技能类型
const (
	CardSkillTypeNone        SkillType = iota
	Refresh                            //刷新
	Gold                               //金币
	AuxiliaryCardCardSubGold           //辅助卡减金币
	DamageSubOrAdd                     //减伤或增伤
	Blood                              //回血
	ObtainCard                         //获得卡牌
	Attack                             //怪兽增伤
	Defense                            //怪兽减伤
)

type Users struct {
	UID                uint            `json:"id"`                 //用户id
	ChairID            int             `json:"chairID"`            //座次号
	Opponent           int             `json:"opponent"`           //对手座位号
	Blood              int64           `json:"blood"`              //血量
	Level              int64           `json:"level"`              //用户等级
	Gold               int64           `json:"gold"`               //金币数
	NextLevelGold      int64           `json:"nextLevelGold"`      //离下一等级金币数
	NextLevelGoldTotal int64           `json:"nextLevelGoldTotal"` //下一等级所需金币数
	Refresh            []RefreshCard   `json:"refresh"`            //刷新牌组
	HandCards          []Card          `json:"handCards"`          //手牌
	SiteCards          []MonstersCard  `json:"siteCards"`          //场地牌
	SkillPrice         int64           `json:"skillPrice"`         //技能价格
	IsSkill            bool            `json:"isSkill"`            //是否激活技能
	Status             UserStatus      `json:"status"`             //人物状态
	SkillType          SkillType       `json:"skillType"`          //技能类型
	SkillRefreshVal    int64           `json:"skillRefreshVal"`    //技能刷新次数
	Battle             []BattleLayout  `json:"battle"`             //对战信息储存
	IsHangUp           bool            `json:"isHangUp"`           //是否挂机
	Ranking            int             `json:"ranking"`            //排名
	Session            *remote.Session `json:"-"`
}
type UserStatus int

const (
	UserStatusIn  UserStatus = iota //游戏中
	UserStatusEnd                   //结算
)

type GameData struct {
	ChairCount  int        `json:"chairCount"`  //总座次人数
	CurBureau   int        `json:"curBureau"`   //当前局数
	GameStatus  GameStatus `json:"gameStatus"`  //游戏状态
	GameStarted bool       `json:"gameStarted"` //是否已经开始
	Tick        int        `json:"tick"`        //倒计时
	Users       []Users    `json:"users"`       //用户组
	//CurChairID  int        `json:"curChairID"`  //当前玩家
	//Loser       []int      `json:"loser"`       //失败
	//Winner      []int      `json:"winner"`      //胜利
}

type GameStatus int

const (
	GameStatusNone GameStatus = iota
	Start                     //进入游戏加载页面
	Select                    //进入选牌页面
	Playing                   //进入对战加载页面
	Battle                    //进入对战页面
	Result                    //进入结算页面
)

type BattleUser struct {
	UID       uint
	ChairID   int
	Status    int   //-1:失败,0:战斗中,1:胜利,2:平局
	BloodLoss int64 //掉血量
	Monsters  []MonstersCard
	Index     int
}

type BattleLayout struct {
	One      BattleUser
	Two      BattleUser
	Status   bool
	IsHandle bool
}

type EndGameUsers struct {
	UID       uint
	Blood     int64
	Level     int64
	Ranking   int
	ChairID   int
	IsSkill   bool
	SkillType SkillType
	HandCards []Card
	SiteCards []MonstersCard
	Battle    []BattleLayout
	IsHangUp  bool
}

type ChHandler struct {
	GameStatus GameStatus      //游戏状态
	Battle     *[]BattleLayout //对战信息
}

const (
	GameSendRefreshCardsPull = 301 //购买刷新牌
	GameSendRefreshPull      = 302 //刷新
	GameSendHandCardsPull    = 303 //操作手牌
	GameSendRSiteCardsPull   = 304 //操作场地牌
	GameSkillPull            = 350 //购买技能
	GameUpgradePull          = 351 //操作升级
	GameStatusPush           = 401 //游戏状态推送
	GameSendRefreshCardsPush = 402 //刷新牌推送
	GameSendHandCardsPush    = 403 //手牌推送
	GameSendSiteCardsPush    = 404 //场地牌推送
	//GameBattlePush          = 444 //分配对战信息推送
	GameBattlePush          = 405 //分配对战信息推送
	GameSkillPush           = 450 //技能推送
	GameUpgradePush         = 451 //升级推送
	GameBloodPush           = 452 //血量推送
	GameBattleSiteCardsPush = 453 //对战卡牌信息推送
	GameGoldPush            = 466 //金币推送
	GameBureauPush          = 480 //局数推送
	GameErrorResponse       = 488 //异常数据推送
	GameUserPush            = 490 //全部用户信息推送
	GameMyUserPush          = 491 //我的用户信息推送
	GameOutPush             = 444 //退出
)

// GameStatusPushData {"type":401,"data":{"gameStatus":1,"tick":3},"pushRouter":"GameMessagePush"}
func GameStatusPushData(gameStatus GameStatus, tick time.Duration, ranking int) any {
	return map[string]any{
		"type": GameStatusPush,
		"data": map[string]any{
			"gameStatus": gameStatus,
			"tick":       tick,
			"ranking":    ranking,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameSendRefreshCardsPushData(handCards []RefreshCard) any {
	return map[string]any{
		"type": GameSendRefreshCardsPush,
		"data": map[string]any{
			"cards": handCards,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameSendHandCardsPushData(handCards []Card) any {
	return map[string]any{
		"type": GameSendHandCardsPush,
		"data": map[string]any{
			"cards": handCards,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameSendSiteCardsPushData(handCards []MonstersCard) any {
	return map[string]any{
		"type": GameSendSiteCardsPush,
		"data": map[string]any{
			"cards": handCards,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameBattlePushData(opponentChairID int, opponent Users) any {
	return map[string]any{
		"type": GameBattlePush,
		"data": map[string]any{
			"opponentChairID": opponentChairID,
			"opponent":        opponent,
		},
		"pushRouter": "GameMessagePush",
	}
}

// GameSkillPushData
// types 1 购买技能 2 使用技能
func GameSkillPushData(types int, desc string) any {
	return map[string]any{
		"type": GameSkillPush,
		"data": map[string]any{
			"type": types,
			"desc": desc,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameUpgradePushData(level, nextLevelGold, nextLevelGoldTotal int64) any {
	return map[string]any{
		"type": GameUpgradePush,
		"data": map[string]any{
			"level":              level,
			"nextLevelGold":      nextLevelGold,
			"nextLevelGoldTotal": nextLevelGoldTotal,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameBloodPushData(loss, blood int64) any {
	return map[string]any{
		"type": GameBloodPush,
		"data": map[string]any{
			"loss":  loss,
			"blood": blood,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameBattleSiteCardsPushData(oneKey, twoKen int, oneBlood, twoBlood int64, oneCards, twoCards []MonstersCard) any {
	return map[string]any{
		"type": GameBattleSiteCardsPush,
		"data": map[string]any{
			"oneKey":       oneKey,
			"twoKey":       twoKen,
			"oneCardBlood": oneBlood,
			"twoCardBlood": twoBlood,
			"oneCards":     oneCards,
			"twoCards":     twoCards,
		},
		"pushRouter": "GameMessagePush",
	}
}

func GameGoldPushData(gold int64) any {
	return map[string]any{
		"type": GameGoldPush,
		"data": map[string]any{
			"gold": gold,
		},
		"pushRouter": "GameMessagePush",
	}
}

// GameBureauPushData {"type":409,"data":{"curBureau":1},"pushRouter":"GameMessagePush"}
func GameBureauPushData(curBureau int) any {
	return map[string]any{
		"type": GameBureauPush,
		"data": map[string]any{
			"curBureau": curBureau,
		},
		"pushRouter": "GameMessagePush",
	}
}
func GameErrorResponseData(result common.Result) any {
	return map[string]any{
		"type":       GameErrorResponse,
		"data":       result,
		"pushRouter": "GameMessagePush",
	}
}

func GameUserPushData(result []Users) any {
	return map[string]any{
		"type":       GameUserPush,
		"data":       result,
		"pushRouter": "GameMessagePush",
	}
}

func GameMyUserPushData(result Users) any {
	return map[string]any{
		"type":       GameMyUserPush,
		"data":       result,
		"pushRouter": "GameMessagePush",
	}
}

func GameOutPushData() any {
	return map[string]any{
		"type":       GameOutPush,
		"data":       nil,
		"pushRouter": "GameMessagePush",
	}
}
